/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ZCalendarBuilder;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.AddInviteData;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImportAppointments
/*     */   extends MailDocumentHandler
/*     */ {
/*  53 */   private static final String[] TARGET_FOLDER_PATH = { "l" };
/*     */   
/*  55 */   protected String[] getProxiedIdPath(Element request) { return TARGET_FOLDER_PATH; }
/*     */   
/*     */ 
/*     */   String DEFAULT_FOLDER_ID;
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  61 */     return false; }
/*     */   
/*  63 */   public ImportAppointments() { this.DEFAULT_FOLDER_ID = "10"; }
/*     */   
/*     */   private static enum SourceSpecMethod {
/*  66 */     ATTACH_ID,  MSG_PART,  INLINE_TEXT;
/*     */     
/*     */     private SourceSpecMethod() {}
/*     */   }
/*     */   
/*  71 */   public Element handle(Element request, Map<String, Object> context) throws ServiceException { ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  72 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  73 */     OperationContext octxt = getOperationContext(zsc, context);
/*     */     
/*  75 */     String folder = request.getAttribute("l", this.DEFAULT_FOLDER_ID);
/*  76 */     ItemId iidFolder = new ItemId(folder, zsc);
/*     */     
/*  78 */     String ct = request.getAttribute("ct");
/*  79 */     if ((!ct.equalsIgnoreCase("ics")) && (!ct.equalsIgnoreCase("text/calendar"))) {
/*  80 */       throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
/*     */     }
/*  82 */     Element content = request.getElement("content");
/*  83 */     List<FileUploadServlet.Upload> uploads = null;
/*  84 */     InputStream is = null;
/*  85 */     String partStr = null;
/*  86 */     String attachment = content.getAttribute("aid", null);
/*  87 */     String messageId = content.getAttribute("mid", null);
/*  88 */     if ((attachment != null) && (messageId != null))
/*  89 */       throw ServiceException.INVALID_REQUEST("use either aid or mid but not both", null);
/*     */     SourceSpecMethod sourceSpecMethod;
/*     */     SourceSpecMethod sourceSpecMethod;
/*  92 */     if (messageId != null) {
/*  93 */       sourceSpecMethod = SourceSpecMethod.MSG_PART; } else { SourceSpecMethod sourceSpecMethod;
/*  94 */       if (attachment != null) {
/*  95 */         sourceSpecMethod = SourceSpecMethod.ATTACH_ID;
/*     */       } else {
/*  97 */         sourceSpecMethod = SourceSpecMethod.INLINE_TEXT;
/*     */       }
/*     */     }
/*     */     try {
/* 101 */       if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod))
/*     */       {
/* 103 */         ItemId iid = new ItemId(messageId, zsc);
/* 104 */         String part = content.getAttribute("part");
/* 105 */         partStr = CreateContact.fetchItemPart(zsc, octxt, mbox, iid, part, null, "utf-8");
/* 106 */         is = new ByteArrayInputStream(partStr.getBytes("utf-8"));
/*     */       }
/*     */       
/*     */ 
/* 110 */       ItemId iidTarget = getProxyTarget(zsc, octxt, iidFolder, true);
/* 111 */       if (iidTarget != null) {
/* 112 */         ZimbraLog.misc.info("Proxying ImportAppointments - folder=" + folder + ", acct=" + iidTarget.getAccountId());
/*     */         FileUploadServlet.Upload up;
/* 114 */         if (SourceSpecMethod.MSG_PART.equals(sourceSpecMethod))
/*     */         {
/* 116 */           content.addAttribute("mid", (String)null);
/* 117 */           content.addAttribute("part", (String)null);
/* 118 */           up = FileUploadServlet.saveUpload(is, "upload.ics", "text/calendar", zsc.getAuthtokenAccountId());
/*     */           
/* 120 */           content.addAttribute("aid", up.getId());
/*     */         }
/* 122 */         return proxyRequest(request, context, iidFolder, iidTarget);
/*     */       }
/*     */       
/* 125 */       if (SourceSpecMethod.ATTACH_ID.equals(sourceSpecMethod)) {
/* 126 */         is = parseUploadedContent(zsc, attachment, uploads = new ArrayList());
/* 127 */       } else if (SourceSpecMethod.INLINE_TEXT.equals(sourceSpecMethod))
/*     */       {
/* 129 */         String text = StringUtil.lfToCrlf(content.getText());
/* 130 */         is = new ByteArrayInputStream(text.getBytes("utf-8"));
/*     */       }
/*     */       
/* 133 */       List<ZCalendar.ZVCalendar> icals = ZCalendar.ZCalendarBuilder.buildMulti(is, "utf-8");
/* 134 */       is.close();
/* 135 */       is = null;
/*     */       
/* 137 */       List<Invite> invites = Invite.createFromCalendar(mbox.getAccount(), null, icals, true, true, null);
/*     */       
/* 139 */       Set<String> uidsSeen = new HashSet();
/* 140 */       StringBuilder ids = new StringBuilder();
/*     */       
/* 142 */       for (Invite inv : invites)
/*     */       {
/* 144 */         uid = inv.getUid();
/* 145 */         if (uid == null) {
/* 146 */           uid = LdapUtil.generateUUID();
/* 147 */           inv.setUid(uid);
/*     */         }
/*     */         boolean addRevision;
/* 150 */         if (!uidsSeen.contains(uid)) {
/* 151 */           boolean addRevision = true;
/* 152 */           uidsSeen.add(uid);
/*     */         } else {
/* 154 */           addRevision = false;
/*     */         }
/*     */         try
/*     */         {
/* 158 */           Mailbox.AddInviteData aid = mbox.addInvite(octxt, inv, iidFolder.getId(), false, addRevision);
/* 159 */           if (aid != null) {
/* 160 */             if (ids.length() > 0) ids.append(",");
/* 161 */             ids.append(aid.calItemId).append("-").append(aid.invId);
/*     */           }
/*     */         } catch (ServiceException e) {
/* 164 */           ZimbraLog.calendar.warn("Skipping bad iCalendar object during import: uid=" + inv.getUid(), e);
/*     */         }
/*     */       }
/*     */       String uid;
/* 168 */       Element response = zsc.createElement(MailConstants.IMPORT_APPOINTMENTS_RESPONSE);
/* 169 */       Element cn = response.addElement("appt");
/* 170 */       cn.addAttribute("ids", ids.toString());
/* 171 */       cn.addAttribute("n", invites.size());
/* 172 */       return response;
/*     */     }
/*     */     catch (IOException e) {
/* 175 */       throw MailServiceException.UNABLE_TO_IMPORT_APPOINTMENTS(e.getMessage(), e);
/*     */     } finally {
/* 177 */       if (is != null)
/* 178 */         try { is.close(); } catch (IOException e) {}
/* 179 */       if (attachment != null) {
/* 180 */         FileUploadServlet.deleteUploads(uploads);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static InputStream parseUploadedContent(ZimbraSoapContext lc, String attachId, List<FileUploadServlet.Upload> uploads) throws ServiceException
/*     */   {
/* 187 */     FileUploadServlet.Upload up = FileUploadServlet.fetchUpload(lc.getAuthtokenAccountId(), attachId, lc.getAuthToken());
/* 188 */     if (up == null)
/* 189 */       throw MailServiceException.NO_SUCH_UPLOAD(attachId);
/* 190 */     uploads.add(up);
/*     */     try {
/* 192 */       return up.getInputStream();
/*     */     } catch (IOException e) {
/* 194 */       throw ServiceException.FAILURE(e.getMessage(), e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ImportAppointments.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */